/**
 * 考勤信息列表attendList.js
 * 版本号： v1.0.0
 */
$(function(){
    var lastChecked = null;
    var projectId = $('#prId_').val();
    $("#attend_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border: false,
        striped:true,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/attend/attendList.do?projectId='+projectId,
        idField: 'person.cardNo',
        frozenColumns:[[
            {field:'person.cardNo',checkbox:true}
        ]],
        columns: [
            [
                {field: 'projectName', title: '项目名称', width: 180, fixed: true,hidden:true},
                {field: 'projectId', title: '项目id', width: 180, fixed: true,hidden:true},
                {field: 'person', title: '身份证号', width: 150, fixed: true,
                    formatter:function(val){
                        if(val){
                            return val.idCard;
                        }
                    }
                },
                {field: '_userName', title: '人员姓名', width: 60, fixed: true,
                    formatter:function(val,row,index){
                        return row.person.name;
                    }
                },
                {field: 'sex', title: '性别', width: 40, fixed: true},
                {field: 'age', title: '年龄', width: 40, fixed: true},
                {field: 'joinProjectTime', title: '加入项目日期', width: 140, fixed: true},
                {field: '_workType', title: '工种', width: 80, fixed: true,
                    formatter:function(val,row,index){
                        return row.person.work.name;
                    }
                },
                {field: 'groupName', title: '班组', width: 120, fixed: true},
                {field: '_deleted', title: '人员状态', width: 80,
                    formatter:function(val,row,index){
                        var deleted = row.person.deleted;
                        if(deleted == 0){
                            return '<span style="color: green; font-weight: bold">在职</span>';
                        }
                        if(deleted == 1){
                            return '<span style="color: red; font-weight: bold">离职</span>';
                        }
                    }
                }
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#attend_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#attend_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            //历史登记施工人数
            document.getElementById("historyPersonCount").innerHTML = data.historyPersonCount;
            //今日出勤人数
            document.getElementById("todayAttendanceCount").innerHTML = data.todayAttendanceCount;
            //离职人数
            document.getElementById("quitedPersonCount").innerHTML = data.quitedPersonCount;
            if(data.rows.length > 0){
                lastChecked = null;
                $("#attend_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#attend_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    })

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            attend_findList();
                        }
                    });
                }
                //详情按钮
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            attend_detail();
                        }
                    });
                }
                //考勤按钮
                if ("detailBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            getAttendDetail();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 查看考勤信息
 * @param index
 */
function getAttendDetail(){
    var checked = $("#attend_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择一条数据后再查看考勤", timeout: 3000, showType: "slide" });
        return;
    };
    loading();
    var row = $('#attend_dataList').datagrid('getSelected');
    var idCard = row.person.idCard;
    var projectId = row.person.projectId;
    var name = row.person.name;
    var deleted = row.person.deleted;
    var joinProjectTime = row.joinProjectTime;
    window.location.href = encodeURI(CONTEXT_PATH + "/attend/toAttendDetailInfo.do?" +
        "idCard="+idCard+"&projectId="+projectId+"&name="+name+"&deleted="+deleted+
        "&joinProjectTime="+joinProjectTime);
}

/**
 * 人员详情
 */
function attend_detail(){
    var checked = $("#attend_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {x
        $.messager.show({ title: '操作提示', msg: "请选择一条数据后再查看人员详情", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $('#attend_dataList').datagrid('getSelected');
    var idCard = row.person.idCard;
    var projectId = row.person.projectId;
    var projectName = row.projectName;
    //IFRAM打开窗口
    window.location.href = encodeURI(ryxq.url+ idCard+"&projectName="+projectName+"&projectId="+projectId);
}

/**
 * 查询考勤信息
 */
function attend_findList(){
    refreshPageNumber("attend_dataList");
    $("#attend_dataList").datagrid("reload",{userName:$("#userName_").textbox("getValue"),deleted:$("#deleted_").combobox("getValue")});
    //清除选中项
    $("#attend_dataList").datagrid("clearSelections");
}
